var Base = require("../common/base");
var modelforum = require("../../models/forum/forum");

class Forum extends Base{
	
	 constructor(){
        super();

		this.hotart = this.hotart.bind(this);
		this.artlist = this.artlist.bind(this);
		this.artinfo = this.artinfo.bind(this);
		this.showreply = this.showreply.bind(this);
		this.addArticle = this.addArticle.bind(this);

        this.msg_success = this.msg_success.bind(this);
		this.msg_err = this.msg_err.bind(this);
		this.msg_success_count = this.msg_success_count.bind(this);
    }

	async hotart(req,res,next){
		// 获取前台页面传过来的参数  
		var param = req.query || req.params;
		modelforum.hotart(param.type,param.num).then(message=>{
			res.send(this.msg_success(message));
		},error=>{
			res.send(this.msg_err(error));
		})
	}


    //文章列表
	async artlist(req,res,next){
		// 获取前台页面传过来的参数  
		var param = req.query || req.params;
		modelforum.article(param.type,param.page,param.num).then(message=>{
			//res.send(this.msg_success_count(message));
			var result = this.msg_success_count(message);
			modelforum.getArticleCount(param.type).then(message=>{
				result.count = message[0]["COUNT(type)"];
				res.send(result);
			},error=>{
				res.send(this.msg_err(error));
			});
		},error=>{
			res.send(this.msg_err(error));
		})
	}

	async artinfo(req,res,next){
		//获取前台页面传过来的参数  
		var param = req.query || req.params;
		modelforum.artinfo(param.id).then(message=>{
			res.send(this.msg_success(message));
		},error=>{
			res.send(this.msg_err(error));
		})
	}

	async showreply(req,res,next){
		//获取前台页面传过来的参数  
		var param = req.query || req.params;
		modelforum.showreply(param.article_id).then(message=>{
			res.send(this.msg_success_count(message));
		},error=>{
			res.send(this.msg_err(error));
		})
	}

	async addArticle(req,res,next){
		//获取前台页面传过来的参数  
		var param = req.query || req.params;
		modelforum.addArticle(param.id,param.author,param.title,param.type,param.desc,param.content,param.click,param.reply).then(message=>{
			res.send(this.msg_insert_success(message));
		},error=>{
			res.send(this.msg_err(error));
		})
	}
   
}

module.exports = new Forum();
